
$(function() {
    window.requestAnimFrame = (function() {
        return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
            window.setTimeout(callback, 1000 / 60)
        }
    })();

    var totalDeg = 360 * 3 + 0;
    var steps = []; 
    var lostDeg = [36, 96, 156, 216, 276,336];
    var prizeDeg = [6, 66, 126,186,246,306];
    var prize, sncode;
	var usenums = 0;
    var count = 0; 
    var now = 0;
    var a = 0.01;
    var outter, inner, timer, running = false;

    function countSteps() {
        var t = Math.sqrt(2 * totalDeg / a);
        var v = a * t;
        for (var i = 0; i < t; i++) {
            steps.push((2 * v * i - a * i * i) / 2)
        }
        steps.push(totalDeg)
    }
     
    function step() {
        outter.style.webkitTransform = 'rotate(' + steps[now++] + 'deg)';
        outter.style.MozTransform = 'rotate(' + steps[now++] + 'deg)';
        if (now < steps.length) { 
            running = true;
            requestAnimFrame(step) 
        } else { 
            running = false;            
             
            setTimeout(function() {
                if (prize != null) {  //中奖
                    $("#sncode").text(sncode);
                    var type = "";
                    if (prize == 1) {
                        type = "一等奖"
                    } else if (prize == 2) {
                        type = "二等奖"
                    } else if (prize == 3) {
                        type = "三等奖"
                    }
                    else if (prize == 4) {
                        type = "四等奖"
                    }
                    else if (prize == 5) {
                        type = "五等奖"
                    }
                    else if (prize == 6) {
                        type = "六等奖"
                    }
                    $("#prizetype").text(type);
                    $("#winprize").val(type);
                    $("#result").slideToggle(500);  
                    $("#outercont").slideUp(500)
                } else { //不中奖
					$("#usenums").text(usenums);
                    alert("Oh,亲，继续努力哦！^_^.")
                }
            }, 200)
        } //if
    } //setps()
    
    function start(deg) {       
        deg = deg || lostDeg[parseInt(lostDeg.length * Math.random())];
        running = true;
        clearInterval(timer);
        totalDeg = 360 * 5 + deg;
        steps = [];
        now = 0;
        countSteps();
        requestAnimFrame(step)
    }
    
    window.start = start;
    outter = document.getElementById('outer'); 
    inner = document.getElementById('inner'); 
    i = 10;
    var end = 0;
    $("#inner").click(function() {

        if (running) return;
        if (count >=10) {
            alert("Oh~No~您已经抽了{viicms:$Dazpan.canrqnums} 次奖,不能再抽了,下次再来吧!");
            return
        }

        if (prize != null) {
            alert("亲，你不能再参加本次活动了喔！下次再来吧^_^");
            return
        }

       $.ajax({
         url     : "randomZhuan.json",
         dataType: "json",
         type    : "POST",
         data    : {
            token  : "{viicms:$Dazpan.token}",
            oneid  : "{viicms:$Dazpan.wecha_id}",
            id     : 1,
            rid    : 1 
         },
         beforeSend : function(){
            running = true;
            timer = setInterval(function() {
                i += 5;
                outter.style.webkitTransform = 'rotate(' + i + 'deg)';
                outter.style.MozTransform = 'rotate(' + i + 'deg)'
            },1)
         },
         success    : function(backdata){
         
            if (backdata.norun == 1) {      
                    alert(backdata.msg);
                    count = 5; //原来是 5 
                    clearInterval(timer);
                    return
            }
            if (backdata.norun == 2) {  
                    alert("您已经抽了"+backdata.canrqnums+"次奖,不能再抽了,下次再来吧!");
                    count = backdata.canrqnums;
                    clearInterval(timer);
                    return
            }
            if (backdata.norun == 3) {
                    alert("请您向公众平台发送抽奖指令，参与抽奖!");
                    clearInterval(timer);
                    return
            }
            
            if (backdata.success) { 
               var prize = backdata.prizetype;
                 if(prize!=''){
               var     sncode = backdata.sn;
                       start(prizeDeg[backdata.prizetype - 1])
                 }else{
                    prize = null;
					usenums=backdata.usenums;
                    start()
                }                    
        
            } else {
                    prize = null;
					usenums=backdata.usenums;
                    start()
            }
            running = false;
            count++;
         },
         error      : function(){
                prize = null;
				 
                start();
                running = false;
                count++;
         },
         complete   : function(XMLHttpRequest, textStatus){
            console.log('当请求完成之后调用这个函数，无论成功或失败.  请求类型:'+textStatus);  
         },
         timeout    : 3000       
        
       })//ajax
    }) //#inner click function;
});

//中奖提交
$("#save-btn").bind("click",
function() {
    var btn = $(this);
    var tel = $("#tel").val();
    var wxname  = $("#wechaname").val();
    var wechaid = $("#wechaid").val();
    var lid     = $("#lid").val();
    var prizetype = $("#winprize").val();
    if (tel == '') {
        alert("请认真输入手机号");
        return
    }
    
    if (wxname == '') {
        alert("请认真输入微信号");
        return
    }
   
    var submitData = {
        lid: lid,
        sncode: $("#sncode").text(),
        tel: tel,
        wxname: wxname,
        wechaid:wechaid,
        prizetype:prizetype,
        action: "add"
    };
    $.post('index.php?g=wap&m=Lottery&a=add', submitData,
    function(data) {
        if (data.success == true) {
            alert(data.msg);
            $("#result").hide("slow");
            return
        } else {}
    },
    "json")
});